﻿//===================================================================
//
// Description: Sparqube Picture Column Lite for SharePoint 2010
// Web: http://www.sparqube.com/
//
//-------------------------------------------------------------------
//
// Copyright (C) 2012 ArtfulBits.CZ (development[at]artfulbits.cz)
// Web: http://www.artfulbits.cz/
//
//-------------------------------------------------------------------
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A 
// PARTICULAR PURPOSE.
//===================================================================

using System;
using System.Web;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace Sparqube.SharePoint.PictureColumnLite
{
	public partial class UploadPicturePage :
		LayoutsPageBase
	{
		#region Fields

		private string m_errorText = "";

		private bool m_closedialog = false;
		private string m_returnValue = "";

		#endregion //Fields

		protected override void OnInit(EventArgs e)
		{
			base.OnInit(e);

			((DialogMaster)this.Page.Master).OkButton.Click += new EventHandler(UploadPicturePage_OKButtonClick);
		}

		protected override void OnPreRender(EventArgs e)
		{
			base.OnPreRender(e);

			if (m_closedialog)
			{
				string script = @"
if (window.parent.frameElement && window.parent.frameElement.commitPopup)
{
    window.parent.frameElement.commitPopup(""" + m_returnValue + @""");
}
else if (window.frameElement && window.frameElement.commitPopup)
{
    window.frameElement.commitPopup(""" + m_returnValue + @""");
}
else 
{
    window.parent.close();
}";
				this.ClientScript.RegisterClientScriptBlock(typeof(UploadPicturePage), "closedialog", script, true);
			}
			else
			{
				if (!string.IsNullOrEmpty(m_errorText))
				{
					LabelError.Text = m_errorText;
				}
			}
		}

		private void UploadPicturePage_OKButtonClick(object sender, EventArgs e)
		{
			HttpPostedFile file;
			HttpFileCollection files = HttpContext.Current.Request.Files;
			if ((files.Count == 0) || (files[0].ContentLength == 0))
			{
				m_errorText = ResourceManager.GetResource(EResource.Error_NoFileUploaded);
			}
			else
			{
				file = files[0];

				string url = "";

				SPList list = SPContext.Current.List;
				string fieldName = this.Page.Request["FieldName"];
				if (null != list)
				{
					PictureLiteField field = list.Fields.GetFieldByInternalName(fieldName) as PictureLiteField;
					if (null != field)
					{
						PictureUploader uploader = new PictureUploader(field, list.ParentWeb);
						try
						{
							url = uploader.UploadPicture(file);
						}
						catch (PictureUploadException ex)
						{
							m_errorText = ex.Message;
						}
						catch (Exception)
						{
							m_errorText = ResourceManager.GetResource(EResource.Error_ErrorSavingPicture);
						}
					}
				}

				if (!string.IsNullOrEmpty(url))
				{
					m_closedialog = true;
					m_returnValue = url;
				}
				else
				{
					if (string.IsNullOrEmpty(m_errorText))
					{
						m_errorText = ResourceManager.GetResource(EResource.Error_ErrorSavingPicture);
					}
				}
			}
		}
	}
}
